/**
 * Main.java
 *
 * Provider: CoderDream's Studio
 *
 * History
 *	Date(DD/MM/YYYY)    Author          Description
 * ----------------------------------------------------------------------------
 *	Nov 5, 2007		CoderDream		Created
 */
package com.coderdream.chapter22.command.sample;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

import com.coderdream.chapter22.command.sample.command.Command;
import com.coderdream.chapter22.command.sample.command.MacroCommand;
import com.coderdream.chapter22.command.sample.drawer.DrawCanvas;
import com.coderdream.chapter22.command.sample.drawer.DrawCommand;

/**
 * <pre>
 * Main.java
 * </pre>
 * 
 */
public class Main extends JFrame implements ActionListener,
		MouseMotionListener, WindowListener {

	// Draw Record
	private MacroCommand history = new MacroCommand();
	// Draw Area
	private DrawCanvas canvas = new DrawCanvas(400, 400, history);
	// Clear button
	private JButton clearButton = new JButton("clear");

	/**
	 * @param title
	 */
	public Main(String title) {
		super(title);

		addWindowListener(this);
		canvas.addMouseMotionListener(this);
		clearButton.addActionListener(this);

		Box buttonBox = new Box(BoxLayout.X_AXIS);
		buttonBox.add(clearButton);

		Box mainBox = new Box(BoxLayout.Y_AXIS);
		mainBox.add(buttonBox);
		mainBox.add(canvas);
		getContentPane().add(mainBox);

		pack();
		setVisible(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == clearButton) {
			history.clear();
			canvas.repaint();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
	 */
	public void mouseDragged(MouseEvent e) {
		Command cmd = new DrawCommand(canvas, e.getPoint());
		history.append(cmd);
		cmd.execute();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
	 */
	public void mouseMoved(MouseEvent e) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
	 */
	public void windowActivated(WindowEvent e) {
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		new Main("Command Pattern Sample");
	}
}
